package com.kaili.tms.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.tms.pojo.dto.SchedulingPlanAddDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanDTO;
import com.kaili.tms.pojo.dto.SchedulingPlanUpdateDTO;
import com.kaili.tms.pojo.entity.SchedulingPlanAddEnity;
import com.kaili.tms.pojo.vo.SchedulingPlanVO;
import com.kaili.tms.service.SchedulingPlanService;
import com.kaili.tms.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @version 1.0
 * @author: xinglei
 * @Time: 19-1-29 下午2:31
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "排班计划管理", description = "排班计划管理API")
public class SchedulingPlanController {

    @Autowired
    private SchedulingPlanService schedulingPlanService;


    @PostMapping(value = "/schedulingPlans")
    @ApiOperation(value = "增加排班计划")
    public RestStatus schedulingPlanAdd(@RequestBody SchedulingPlanAddDTO schedulingPlanAddDTO) {
        SchedulingPlanAddEnity schedulingPlanAdd = new SchedulingPlanAddEnity();
        BeanUtils.copyProperties(schedulingPlanAddDTO, schedulingPlanAdd);
        schedulingPlanAdd.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
        schedulingPlanAdd.setCreatedTime(new Date());
        Integer statu = schedulingPlanService.schedulingPlanAdd(schedulingPlanAdd);
        if (statu == 1) {
            return CommonStatus.TMS_SCHEDULINGPLAN_ADD_FAIL;
        }
        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/schedulingPlans")
    @ApiOperation(value = "修改排班计划")
    public RestStatus schedulingPlanUpdate(@RequestBody SchedulingPlanUpdateDTO schedulingPlan0UpdateDTO) {
        schedulingPlanService.schedulingPlanUpdate(schedulingPlan0UpdateDTO);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping(value = "/schedulingPlans")
    @ApiOperation(value = "分页查询排班计划")
    public Page<SchedulingPlanVO> schedulingPlanGetAll(@ModelAttribute SchedulingPlanDTO schedulingPlanDTO) {
        if (LoginDetailUtils.getSiteId() != 1) {
            schedulingPlanDTO.setSiteId(LoginDetailUtils.getSiteId().longValue());
        }
        Page<SchedulingPlanVO> pager = new Page<>(schedulingPlanDTO.getPage(), schedulingPlanDTO.getPageSize());
        return schedulingPlanService.queryschedulingPlan(pager, schedulingPlanDTO);
    }

    @DeleteMapping(value = "/schedulingPlans/{planId}")
    @ApiOperation(value = "删除排班计划")
    public RestStatus schedulingPlanDelete(@PathVariable Long planId) {
        Integer a = schedulingPlanService.schedulingPlanDelete(planId);
        if (a == 1) {
            //400 证明排版计划明细里面已经有执行过的了 所以不能进行删除修改
            return CommonStatus.DELETE_FAIL;
        }
        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/schedulingPlans/code")
    @ApiOperation(value = "得到排班编号")
    public String schedulingPlanCode() {
        String vehicleCode = schedulingPlanService.schedulingPlanCode(LoginDetailUtils.getSiteId());
        return vehicleCode;
    }
}
